use lib "../";
use BuildTool;

use Test::More tests => 5;
use Test::Exception;
Test::More->builder->output ('Test_Report/Test_Cases/ts3_result.txt');
Test::More->builder->failure_output ('Test_Report/Test_Cases/ts3_errors.txt');
 
 BEGIN {
	use_ok(VCS);
 	use_ok(BuildTool);
 }
 
my $username;
 
subtest 'Test Case 3.1' => sub {
	plan tests => 1;
####################################################################################
#	
# Verifying that release_history file exists when it's not the first cron or baseline
#
####################################################################################
	#Path to the release_history file in the repository
	my $vcs_path = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS3/TC3_1";
    open(CMD,"whoami |");
	$username = <CMD>;
	chomp($username);
	VCS->checkOutRelHis($vcs_path,$username);
	is(BuildTool->checkIsFirstBuild("TC3_1/rel_h.txt"),"false", "verifying if it\'s NOT the first build");
	system("rm -rf TC3_1");
};

subtest 'Test Case 3.2' => sub {
	plan tests => 6;
#######################################################################################
#	
# Verifying that the buildInfo is correct and related to the
# previous baseline. Current build: baseline
#
#######################################################################################
	#-------------------------------------------------------------------------------------
	# Scenario 1: Previous build is a baseline
	#-------------------------------------------------------------------------------------
	my $vcs_path  = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS3/TC3_2/TC3_2_1";
	my $type = "baseline";
	my $buildInfo = "buildInfo.txt";
	VCS->checkOutRelHis($vcs_path . "/tags",$username);
	#Check if it's getting the correct previous build name
	my @prev_type_first_build = BuildTool->getPrevBuildName("tags/rel_h.txt",$type,$vcs_path);
	system("rm -rf tags");
	is($prev_type_first_build[0],"base1.1","Verifying that the previous build is a baseline");
	BuildTool->getBuildInfo($prev_type_first_build[0],$buildInfo,$vcs_path);
	open(FILE1,$buildInfo);
	my @build_info = <FILE1>;
	close FILE1;
	system ("rm -rf buildInfo.txt");
	#Verifying if build info is equal to expected
	is($build_info[0],"http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/assert.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/buildlib.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/config.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/create_lists.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/dirbuild.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/includes.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/libs.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/rules.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/scatter.scat http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/scatter_for_rtl.scat http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/all_var.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/bootup_main_shared.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/fillcode.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/retargeted_libs.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/pack.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/static_assert.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/std_defs.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_const.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_loc.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_methods.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_popdesign.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/BuildTools/GetCommitedRev.pl http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/BuildTools/galaxy_hexcrc.pl http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/MapCreator/GalaxyParameters.txt http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/MapCreator/MapCreator.pl\n");
	is($build_info[1], "9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9906 9889 9889 9889 9889 9889 9889 9889 9889 9889 ");
	#-------------------------------------------------------------------------------------
	#Scenario 2: Previous build is a cron
	#-------------------------------------------------------------------------------------
	my $vcs_path  = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS3/TC3_2/TC3_2_2";
	my $type = "baseline";
	my $buildInfo = "buildInfo.txt";
	VCS->checkOutRelHis($vcs_path . "/tags",$username);
	#Check if it's getting the correct previous build name
	my @prev_type_first_build = BuildTool->getPrevBuildName("tags/rel_h.txt",$type,$vcs_path);
	system("rm -rf tags");
	is($prev_type_first_build[0],"base1.1","Verifying that the previous build is a baseline");
	BuildTool->getBuildInfo($prev_type_first_build[0],$buildInfo,$vcs_path);
	open(FILE1,$buildInfo);
	my @build_info = <FILE1>;
	close FILE1;
	system ("rm -rf buildInfo.txt");
	#Verifying if build info is equal to expected
	is($build_info[0],"http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/assert.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/buildlib.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/config.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/create_lists.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/dirbuild.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/includes.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/libs.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/rules.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/scatter.scat http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/scatter_for_rtl.scat http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/all_var.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/bootup_main_shared.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/fillcode.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/retargeted_libs.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/pack.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/static_assert.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/std_defs.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_const.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_loc.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_methods.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_popdesign.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/BuildTools/GetCommitedRev.pl http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/BuildTools/galaxy_hexcrc.pl http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/MapCreator/GalaxyParameters.txt http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/MapCreator/MapCreator.pl\n");
	is($build_info[1], "9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9906 9889 9889 9889 9889 9889 9889 9889 9889 9889 ");
};

subtest 'Test Case 3.3' => sub {
	plan tests => 6;
#######################################################################################
#	
# Verifying that the buildInfo is correct and related to the
# previous build. Current build: cron
#
#######################################################################################
	#-------------------------------------------------------------------------------------
	# Scenario 1: Previous build is a cron
	#-------------------------------------------------------------------------------------
	my $vcs_path  = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS3/TC3_3/TC3_3_1";
	my $type = "cron";
	my $buildInfo = "buildInfo.txt";
	VCS->checkOutRelHis($vcs_path . "/tags",$username);
	#Check if it's getting the correct previous build name
	my @prev_type_first_build = BuildTool->getPrevBuildName("tags/rel_h.txt",$type,$vcs_path);
	system("rm -rf tags");
	is($prev_type_first_build[0],"cron1.2","Verifying that the previous build is a cron");
	BuildTool->getBuildInfo($prev_type_first_build[0],$buildInfo,$vcs_path);
	open(FILE1,$buildInfo);
	my @build_info = <FILE1>;
	close FILE1;
	system ("rm -rf buildInfo.txt");
	#Verifying if build info is equal to expected
	is($build_info[0],"http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/assert.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/buildlib.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/config.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/create_lists.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/dirbuild.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/includes.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/libs.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/rules.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/scatter.scat http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/scatter_for_rtl.scat http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/all_var.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/bootup_main_shared.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/fillcode.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/retargeted_libs.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/pack.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/static_assert.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/std_defs.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_const.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_loc.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_methods.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_popdesign.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/BuildTools/GetCommitedRev.pl http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/BuildTools/galaxy_hexcrc.pl http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/MapCreator/GalaxyParameters.txt http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/MapCreator/MapCreator.pl\n");
	is($build_info[1], "9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9906 9889 9889 9889 9889 9889 9889 9889 9889 9889 ");
	#-------------------------------------------------------------------------------------
	#Scenario 2: Previous build is a baseline
	#-------------------------------------------------------------------------------------
	my $vcs_path  = "http://cad-test/repo/crdm/device/firmware/tools/build_tool/tags/quals/TS3/TC3_3/TC3_3_2";
	my $type = "cron";
	my $buildInfo = "buildInfo.txt";
	VCS->checkOutRelHis($vcs_path . "/tags",$username);
	#Check if it's getting the correct previous build name
	my @prev_type_first_build = BuildTool->getPrevBuildName("tags/rel_h.txt",$type,$vcs_path);
	system("rm -rf tags");
	is($prev_type_first_build[0],"base1.1","Verifying that the previous build is a baseline");
	BuildTool->getBuildInfo($prev_type_first_build[0],$buildInfo,$vcs_path);
	open(FILE1,$buildInfo);
	my @build_info = <FILE1>;
	close FILE1;
	system ("rm -rf buildInfo.txt");
	#Verifying if build info is equal to expected
	is($build_info[0],"http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/assert.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/buildlib.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/config.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/create_lists.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/dirbuild.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/includes.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/libs.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/rules.mk http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/scatter.scat http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/build/scatter_for_rtl.scat http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/all_var.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/bootup_main_shared.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/fillcode.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_app/retargeted_libs.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/pack.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/static_assert.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/shared_system/std_defs.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/makefile http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_const.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_loc.h http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_methods.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/code/subsystem/subsystem_popdesign.c http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/BuildTools/GetCommitedRev.pl http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/BuildTools/galaxy_hexcrc.pl http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/MapCreator/GalaxyParameters.txt http://cad-test/repo/crdm/device/firmware/tools/build_tool/trunk/quals/main_fw/tools/MapCreator/MapCreator.pl\n");
	is($build_info[1], "9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9889 9906 9889 9889 9889 9889 9889 9889 9889 9889 9889 ");
};